//
//  ZZPersonHeadView.swift
//  QMXK
//
//  Created by jenkin on 2018/12/6.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZPersonHeadView: UITableViewHeaderFooterView {
    
    var backView:UIView!
    var elasticShaperLayer:CAShapeLayer!
    var rate:CGFloat = CommenConst.rate
    var navH:CGFloat = CommenConst.KNAVH
    var kscreenW:CGFloat = CommenConst.KScreenW
    let CONTENTOFFSET_KEYPATH:String = "contentOffset"
    
    var tableView:UITableView!
    var scrollView:UITableView!{
        get{
            return self.tableView
        }
        set{
            if self.tableView != nil{
                self.tableView.removeObserver(self, forKeyPath: CONTENTOFFSET_KEYPATH)
            }
            self.tableView = newValue
            self.tableView.addObserver(self, forKeyPath: CONTENTOFFSET_KEYPATH, options: NSKeyValueObservingOptions.initial, context: nil)
        }
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        self.setUI()
    }
    
    func setUI(){
        
        self.backView = UIView.init()
        self.contentView.addSubview(self.backView)
        self.backView.backgroundColor = UIColor.orange
        self.backView.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalTo(0)
        }
        
        //添加贝塞尔曲线
        self.addBeth()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func addBeth(){
        
        self.elasticShaperLayer = CAShapeLayer.init(layer: self.layer)
        self.elasticShaperLayer.path = self.calculateAnimaPathWithOriginY(orginY: 0.0)
        self.elasticShaperLayer.fillColor = UIColor.white.cgColor
        self.backView.layer.addSublayer(self.elasticShaperLayer)
    }
    
    
    func calculateAnimaPathWithOriginY(orginY:CGFloat) -> CGPath{
        
        let beginY:CGFloat = self.rate * 300.0 + self.navH
        let endY:CGFloat = self.rate * 300.0 + self.navH - self.rate * 198
        let centerY:CGFloat = endY - self.rate * 100 + orginY
        let leftBeginPoint:CGPoint = CGPoint.init(x: 0, y: beginY)
        let rightBeginPoint:CGPoint = CGPoint.init(x: self.kscreenW, y: beginY)
        let letfEndPoint:CGPoint = CGPoint.init(x: 0, y: endY)
        let rightEndPoint:CGPoint = CGPoint.init(x: self.kscreenW, y: endY)
        let centerYPoint:CGPoint = CGPoint.init(x: self.kscreenW / 2, y: centerY)
    
        let bezierleth:UIBezierPath! = UIBezierPath.init()
        bezierleth.move(to: leftBeginPoint)
        bezierleth.addLine(to: letfEndPoint)
        bezierleth.addQuadCurve(to: rightEndPoint, controlPoint: centerYPoint)
        bezierleth.addLine(to: rightBeginPoint)
        bezierleth.addLine(to: leftBeginPoint)
        return bezierleth.cgPath
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == CONTENTOFFSET_KEYPATH{
            
            let offsetY:CGFloat = self.tableView.contentOffset.y
            print(offsetY)
            if self.tableView.isDragging || offsetY > -self.rate * 100 {
                if offsetY < 0{
                    return;
                }
                
                if offsetY > self.rate * 200{
                    return;
                }
                
                self.elasticShaperLayer.path = self.calculateAnimaPathWithOriginY(orginY: offsetY)
            }
        }
    }
    
    deinit {
        self.tableView.removeObserver(self, forKeyPath: CONTENTOFFSET_KEYPATH)
    }
}
